package com.bohua.intermediary.utils;

import com.alibaba.fastjson.JSON;

import com.bohua.intermediary.common.BaiDuAccessToken;
import com.bohua.intermediary.common.BaiDuZhi;
import org.springframework.http.HttpEntity;
import org.springframework.http.HttpHeaders;
import org.springframework.web.client.RestTemplate;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 *操作工具
 */
public class FaceUtils {


    private static RestTemplate restTemplate =new RestTemplate();

    public static String getAuth(){
        // 官网获取的 API Key 更新为你注册的
        String clientId = "YhGZet169QMZ0XGfGg6GgXVR";
        // 官网获取的 Secret Key 更新为你注册的
        String clientSecret = "ljjFC8392McKT2MuzHHjw1v4OY2Ggkh4";
        return getAuth(clientId, clientSecret);
    }

    /**
     * 获取API访问token
     * 该token有一定的有效期，需要自行管理，当失效时需重新获取.
     * @param ak - 百度云官网获取的 API Key
     * @param sk - 百度云官网获取的 Securet Key
     * @return assess_token 示例：
     * "24.460da4889caad24cccdb1fea17221975.2592000.1491995545.282335-1234567"
     */
    public static String getAuth(String ak, String sk) {
        // 获取token地址
        String authHost = "https://aip.baidubce.com/oauth/2.0/token?";
        String getAccessTokenUrl = authHost
                // 1. grant_type为固定参数
                + "grant_type=client_credentials"
                // 2. 官网获取的 API Key
                + "&client_id=" + ak
                // 3. 官网获取的 Secret Key
                + "&client_secret=" + sk;
        try {
//            RestTemplate template=new RestTemplate();

            BaiDuAccessToken accessToken= restTemplate.getForObject(getAccessTokenUrl,BaiDuAccessToken.class);
            System.out.println("tokenzhi:"+JSON.toJSONString(accessToken));


           return accessToken.getAccess_token();
        } catch (Exception e) {
            System.err.printf("获取token失败！");
            e.printStackTrace(System.err);
        }
        return null;
    }

    public static BaiDuZhi ContrastFace(String image){
        String lujing="https://middlemanage.oss-cn-beijing.aliyuncs.com/imagezhi/one.png?Expires=1550362246&OSSAccessKeyId=TMP.AQHAPuE1ijdHwxQkAXlq2b4Re8ndAuZAoVVU70g524RDzRxqXintrLdOJraZADAtAhUA_OJ9SpNmpxcQbaiEr0aMgLJ_p7MCFCYMS5lhk-B1MA2Sa2V-QMEq4M4G&Signature=1nRxblIK%2FSJ%2FqNAInAKBX7LZqzY%3D";
        String basezhi= Base64Util.imageToBase64(lujing);

        HttpHeaders requestHeaders = new HttpHeaders();
        requestHeaders.add("Content-Type", "application/json");

        List<Map<String, Object>> images = new ArrayList<>();

        Map<String, Object> map1 = new HashMap<>();
        map1.put("image", basezhi);
        map1.put("image_type", "BASE64");
        map1.put("face_type", "LIVE");
        map1.put("quality_control", "LOW");
        map1.put("liveness_control", "NORMAL");

        Map<String, Object> map2 = new HashMap<>();
        map2.put("image", image);
        map2.put("image_type", "BASE64");
        map2.put("face_type", "LIVE");
        map2.put("quality_control", "LOW");
        map2.put("liveness_control", "NONE");

        images.add(map1);
        images.add(map2);

        String jsonBodyZhi=JSON.toJSONString(images);
        System.out.println("对比值："+jsonBodyZhi);

        //body

        //HttpEntity
        HttpEntity<String> requestEntity = new HttpEntity<String>(jsonBodyZhi, requestHeaders);

        String actokenzhi=FaceUtils.getAuth();

        //post
        BaiDuZhi baiDuZhi =restTemplate.postForObject("https://aip.baidubce.com/rest/2.0/face/v3/match?access_token="+actokenzhi, requestEntity, BaiDuZhi.class);
        System.out.println("最终值："+JSON.toJSONString(baiDuZhi));

        return baiDuZhi;

    }



}
